package cn.wolfcode.util;

import cn.wolfcode.domain.Employee;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;


import javax.servlet.http.HttpSession;
import java.util.List;

public class UserContext {

    public static final String USER_IN_SESSION = "USER_IN_SESSION";
    public static final String PERMISSION_IN_SESSION = "PERMISSION_IN_SESSION";

    /**
     * 使用springMVC 提供的工具类 获取session 对象
     * @return
     */
    public static HttpSession getSession(){
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder
                .getRequestAttributes();
        return requestAttributes.getRequest().getSession();
    }

    /**
     * 获取用户session
     * @return
     */
    public static Employee getCurrentUser() {

        return (Employee) getSession().getAttribute("USER_IN_SESSION");
    }

    /**
     * 设置用户session
     * @return
     * @param employee
     */
    public static void setCurrentUser(Employee employee) {
        getSession().setAttribute("USER_IN_SESSION", employee);

    }

    /**
     * 获取权限session
     * @return
     */
    public static List<String> getPermissionExpression() {



        return (List<String>) getSession().getAttribute("PERMISSION_IN_SESSION");
    }

    /**
     * 设置权限session
     * @return
     * @param permissionExpressions
     */
    public static void setPermissionExpression(List<String> permissionExpressions) {

        getSession().setAttribute("PERMISSION_IN_SESSION",permissionExpressions);
    }
}
